<?php
require_once "../../php/config/config.php";
require_once "../model/adminDb.php";
$AdminDb = new AdminDb();
$name = $_SESSION['name'];
$dat = $AdminDb->query_userName($name);
$card = $dat[0]['card'];


?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="refresh" content="600">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/js/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/css/view.css"/>
    <title>管理后台</title>
</head>
<body class="layui-view-body">
<div class="layui-content">
    <div class="layui-row">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="form-box">

                    <?php
                    if($card==1){
                        echo "<button class=\"layui-btn layui-btn-sm \" onclick=\"add_user()\"><i class=\"layui-icon\">&#xe654;</i> 添加用户
                    </button>";

                    }

                    ?>

                    <button class="layui-btn layui-btn-primary layui-btn-sm" id="refresh" onclick="refresh()"><i
                                class="layui-icon">&#xe9aa;</i>刷新
                    </button>
                    <input type="text" name="name" id="name" disabled value="<?php echo $name ?>" style="display: none">
                    <input type="text" name="userCard" id="userCard" disabled value="<?php echo $card ?>" style="display: none">
                    <table id="userList" lay-filter="userList"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../../static/js/jquery-3.3.1.min.js"></script>
<script src="../../static/js/layui/layui.all.js"></script>
<script>
    //获取用户名
    var edit;
    $name = $("#name").val();
    $userCard = $("#userCard").val();
    //显示用户列表
    layui.use('table', function () {
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#userList'
            , url: '../controller/query_user.php' //数据接口
            , page: true //开启分页
            , method: 'post'
            , cols: [[ //表头
                {field: 'id', title: 'id', width: 80, sort: true, fixed: 'left', toolbar: '#tableId'}
                , {field: 'name', title: '姓名/账号', width: 150}
                , {field: 'card', title: '角色', width: 150, toolbar: '#card'}
                , {field: 'addtime', title: '添加时间', toolbar: '#time', width: 180}
                , {field: '', toolbar: '#barDemo',width:250,fixed:'right', title: '操作'}
            ]]
            , done: function (res, curr, count) {
                console.log(res);
                if (res.count == 0) {
                    layer.msg("没有符合条件的数据哦");
                }
            }
        });


        //监听行工具事件
        table.on('tool(userList)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                $id = data.id;//获取当前一行的id

                layer.alert(data.desction, {
                    skin: 'layui-layer-molv' //样式类名
                    , closeBtn: 0

                });

            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    $id = data.id;//获取当前一行的id
                    delUser($id);
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'edit') {
                $id = data.id;//获取当前一行的id

                editUser($id);

            }
        });


    });

    //js将php时间戳转换成格式化日期
    function to_date(phpstr) {
        str = parseInt(phpstr) * 1000;//将php时间戳转化为整形并乘以1000
        var newDate = new Date(str);
        var year = newDate.getUTCFullYear();//取年份
        var month = newDate.getUTCMonth() + 1;//取月份
        var nowday = newDate.getUTCDate();//取天数
        var hours = newDate.getHours();//取小时
        var minutes = newDate.getMinutes();//取分钟
        var seconds = newDate.getSeconds();//取秒
        return year + "-" + month + "-" + nowday + " " + hours + ":" + minutes + ":" + seconds;//拼接 2017-2-21 12:23:43    }

    }

    //添加用户
    function add_user() {

        layer.open({
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: true,
            content: 'users.html'
        });


    }

    //编辑用户

    //添加用户
    function editUser(val) {

        edit=layer.open({
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: true,
            content: 'editUser.php?id=' + val,
        });


    }

    //删除用户
    //删除
    function delUser(id, index, obj) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/del_user.php',
            data: {
                "id": id
            },
            dataType: 'json',
            success: function (res) {
                layer.msg(res.msg);
                if (res.code == 200) {
                    layer.close(index);
                    obj.del(); //删除对应行（tr）的DOM结构
                }

            },
            error: function (res) {
                layer.msg("删除失败");
            }


        });

    }

    //点击刷新
    function refresh() {
        window.location.reload();

    }

    function close() {
        edit.layer.close();
    }

</script>

<script type="text/html" id="time">

    <a style="color: #787878" class="layui-table-link">{{to_date(d.addtime)}}</a>
</script>
<script type="text/html" id="tableId">

    <a style="color: #787878" class="layui-table-link" lay-event="detail">{{d.id}}</a>
</script>
<script type="text/html" id="card">


    {{#  if(d.card =="1"){ }}

    <button style="color: red" class="layui-btn layui-btn-primary layui-btn-xs">
        <i class="layui-icon">&#xe66c;</i>管理员
    </button>
    {{#  } }}
    {{#  if(d.card ==2){ }}

    <button class="layui-btn layui-btn-primary layui-btn-xs">
        <i class="layui-icon">&#xe66f;</i>用户
    </button>
    {{#  } }}
</script>
<script type="text/html" id="barDemo">
    {{#  if($userCard =="1"){ }}
    {{#  if(d.name ==$name){ }}

    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs  layui-btn-danger" lay-event="del">删除</a>
    {{#  } }}
    {{#  if(d.card ==2){ }}

    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs  layui-btn-danger" lay-event="del">删除</a>

        {{#  } }}
    {{#  } }}
    {{#  if($userCard =="2"){ }}


        {{#  if(d.card ==2){ }}
        {{#  if(d.name ==$name){ }}
        <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="edit">编辑</a>

        {{#  } }}


        {{#  } }}



    {{#  } }}
    <!--    {{#  if(d.card =="1"){ }}-->
    <!--    {{#  if(d.name ==$name){ }}-->
    <!--    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>-->
    <!--    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
    <!--    <a class="layui-btn layui-btn-xs  " lay-event="">删除</a>-->
    <!--    {{#  } }}-->
    <!--    {{#  if(d.name !=$name){ }}-->
    <!--    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>-->
    <!--    <a class="layui-btn layui-btn-xs layui-btn-disabled" lay-event="">编辑</a>-->
    <!--    <a class="layui-btn layui-btn-xs  layui-btn-disabled" lay-event="">删除</a>-->
    <!--    {{#  } }}-->
    <!--    {{#  } }}-->
    <!---->
    <!--    {{#  if(d.card ==2){ }}-->
    <!--    {{#  if(d.name ==$name){ }}-->
    <!--    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>-->
    <!--    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
    <!--    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled" lay-event="">删除</a>-->
    <!---->
    <!--    {{#  } }}-->
    <!--    {{#  if(d.name !=$name){ }}-->
    <!--    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>-->
    <!--    <a class="layui-btn layui-btn-xs layui-btn-disabled" lay-event="">编辑</a>-->
    <!--    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled" lay-event="">删除</a>-->
    <!--    {{#  } }}-->
    <!---->
    <!--    {{#  } }}-->
</script>
</body>
</html>